<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p id="chat-room">

</p>
<input id="inp-msg"/>
<button id="btn-send">发送</button>
</body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script>
    var app = {
        url : null,
        user : null,
        init : function(url, user){
            app.url = url
            app.user = {
                name : user
            }
            app.receive()
        },
        send : function (msg) {
            $.post(app.url + "/jms/send", msg, function (result) {
                if(result == 'send success'){
                    $('#chat-room').append(msg.formUser.name + " : " + msg.msg + "\n")
                }
            })
        },
        receive : function () {
            var receive = {
                toUser : app.user
            }
            while (true){
                $.post(app.url + '/jms/receive', receive, function (msg) {
                    $('#chat-room').append(msg.formUser.name + " : " + msg.msg + "\n")
                })
            }
        }
    }
    $(function () {
        $('#btn-send').click(function () {
            var inp_text = $('#inp-msg').val()
            if(app.user == null){
                app.init('http://localhost:8080', inp_text)
            } else {
                var msg = {
                    formUser : app.user,
                    toUser : 'all',
                    msg : inp_text
                }
                app.send(msg)
            }
        })
    })
</script>
</html>
